package com.lanyou.cook.paramsetting;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.datadictionary.UserLogOpt;
import com.lanyou.cook.paramsetting.vo.SmsSetting;
import com.lanyou.cook.paramsetting.vo.SmtpSetting;
import com.lanyou.cook.service.UserLogService;
import com.lanyou.cook.service.security.UserSessionService;

/**
 * 邮件服务设置相关业务控制层
 * @author 曾族根
 *
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/paramSettings")
public class ParamSettingController {
	@Autowired
	ParamSettingService paramSettingService;
	
	@Autowired
	private UserLogService userLogService;
	
	/**
	 * 修改邮件相关的设置
	 * @param param 类型:ParamSettingParam 邮件服务设置的参数实体
	 */
	@RequiresPermissions(value={"paramSetting:maintain"})
	@RequestMapping(value = "/smtp", method=RequestMethod.POST)
	@Transactional
	public void updateSmtpSetting(@RequestBody SmtpSetting param){
		String userCode = UserSessionService.getCurrentAuthenticatedUserCode();
		paramSettingService.saveObject(Keys.SMTP, param);
		userLogService.save(UserLogOpt.EmailServiceSetting, null,
				String.format("用户“%s”修改了邮件服务设置信息", userCode));
	}
	
	/**
	 * 获取邮件服务设置信息
	 * @return 类型:String 邮件服务设置信息
	 */
	@RequiresPermissions(value={"paramSetting:maintain"})
	@RequestMapping(value = "/smtp")
	public String  getSmtpSetting(){
		return paramSettingService.getAsJson(Keys.SMTP).toString();
	}
	
	
	/**
	 * 修改服务日志管理相关的设置
	 * @param param 类型:ParamSettingParam 服务日志设置的参数实体
	 */
	@RequiresPermissions(value={"paramSetting:maintain"})
	@RequestMapping(value = "/sms", method=RequestMethod.POST)
	@Transactional
	public void updateSmsSetting(@RequestBody SmsSetting param){
		String userCode = UserSessionService.getCurrentAuthenticatedUserCode();
		paramSettingService.saveObject(Keys.SMS, param);
		userLogService.save(UserLogOpt.EmailServiceSetting, null,
				String.format("用户“%s”修改了邮件服务设置信息", userCode));
	}
	
	/**
	 * 获取服务日志设置设置信息
	 * @return 类型:String 服务日志设置信息
	 */
	@RequiresPermissions(value={"paramSetting:maintain"})
	@RequestMapping(value = "/sms")
	public String  getSmsSetting(){
		return paramSettingService.getAsJson(Keys.SMS).toString();
	}
	
	
}
